//
//  KLCurrencyDAO.m
//  KLCurrency
//
//  Created by apple on 2017/12/25.
//  Copyright © 2017年 bonc. All rights reserved.
//

#import "KLCurrencyDAO.h"

@implementation KLCurrencyDAO

+ (long long)insertModel:(KLCurrencyEntity *)model withDataBase:(FMDatabase *)db {
    BOOL success = [db executeUpdate:@"insert into 'TB_CURRENCY_ENTITY' ('name', 'nation', 'abbreviation', 'symbol', 'main', 'common', 'local') values (?,?,?,?,?,?,?)",model.name,model.nation,model.abbreviation,model.symbol,@(model.main),@(model.common),@(model.local)];
    if (success) {
        return [db lastInsertRowId];
    } else {
        return 0;
    }
}

+ (long long)queryModelCountWithDataBase:(FMDatabase *)db {
    long long count = [db longForQuery:@"select count (*) from 'TB_CURRENCY_ENTITY'"];
    return count;
}

+ (KLCurrencyEntity *)queryModelById:(long long)localId withDataBase:(FMDatabase *)db {
    FMResultSet *resultSet = [db executeQuery:@"select * from 'TB_CURRENCY_ENTITY' where id = ?", @(localId)];
    if ([resultSet next]) {
        KLCurrencyEntity *entity = [[KLCurrencyEntity alloc] initWithFMResultSet:resultSet];
        [resultSet close];
        return entity;
    }
    return nil;
}

+ (KLCurrencyEntity *)queryModelByAbbreviation:(NSString *)abbr withDataBase:(FMDatabase *)db {
    FMResultSet *resultSet = [db executeQuery:@"select * from 'TB_CURRENCY_ENTITY' where abbreviation = ?", abbr];
    if ([resultSet next]) {
        KLCurrencyEntity *entity = [[KLCurrencyEntity alloc] initWithFMResultSet:resultSet];
        [resultSet close];
        return entity;
    }
    return nil;
}

+ (KLCurrencyEntity *)queryModelByNation:(NSString *)nation withDataBase:(FMDatabase *)db {
    FMResultSet *resultSet = [db executeQuery:@"select * from 'TB_CURRENCY_ENTITY' where nation = ?", nation];
    if ([resultSet next]) {
        KLCurrencyEntity *entity = [[KLCurrencyEntity alloc] initWithFMResultSet:resultSet];
        [resultSet close];
        return entity;
    }
    return nil;
}

+ (BOOL)updateModelWithAbbreviation:(NSString *)abbr mainValue:(BOOL)mainValue withDataBase:(FMDatabase *)db {
    BOOL success = [db executeUpdate:@"update 'TB_CURRENCY_ENTITY' set main = ? where abbreviation = ?",@(mainValue),abbr];
    return success;
}

+ (BOOL)updateModelWithAbbreviation:(NSString *)abbr commonValue:(BOOL)commonValue withDataBase:(FMDatabase *)db {
    BOOL success = [db executeUpdate:@"update 'TB_CURRENCY_ENTITY' set common = ? where abbreviation = ?",@(commonValue),abbr];
    return success;
}

+ (BOOL)updateModelWithNation:(NSString *)nation localValue:(BOOL)localValue withDataBase:(FMDatabase *)db {
    BOOL success = [db executeUpdate:@"update 'TB_CURRENCY_ENTITY' set local = ? where nation = ?",@(localValue),nation];
    return success;
}

+ (BOOL)deleteModelWithId:(long long)localId withDataBase:(FMDatabase *)db {
    BOOL success = [db executeUpdate:@"delete from 'TB_CURRENCY_ENTITY' where id = ?",@(localId)];
    return success;
}

+ (BOOL)deleteModelWithAbbreviation:(NSString *)abbr withDataBase:(FMDatabase *)db {
    BOOL success = [db executeUpdate:@"delete from 'TB_CURRENCY_ENTITY' where abbreviation = ?",abbr];
    return success;
}

+ (NSArray<KLCurrencyEntity *> *)queryAllModelsWithDataBase:(FMDatabase *)db {
    NSMutableArray *results = [@[] mutableCopy];
    FMResultSet *resultSet = [db executeQuery:@"select * from 'TB_CURRENCY_ENTITY'"];
    while ([resultSet next]) {
        KLCurrencyEntity *entity = [[KLCurrencyEntity alloc] initWithFMResultSet:resultSet];
        [results addObject:entity];
    }
    [resultSet close];
    return results;
}

+ (NSArray<KLCurrencyEntity *> *)queryCommonModelsWithDataBase:(FMDatabase *)db {
    NSMutableArray *results = [@[] mutableCopy];
    FMResultSet *resultSet = [db executeQuery:@"select * from 'TB_CURRENCY_ENTITY' where common = 1"];
    while ([resultSet next]) {
        KLCurrencyEntity *entity = [[KLCurrencyEntity alloc] initWithFMResultSet:resultSet];
        [results addObject:entity];
    }
    [resultSet close];
    return results;
}

+ (NSArray<KLCurrencyEntity *> *)queryLocalModelsWithDataBase:(FMDatabase *)db {
    NSMutableArray *results = [@[] mutableCopy];
    FMResultSet *resultSet = [db executeQuery:@"select * from 'TB_CURRENCY_ENTITY' where local = 1"];
    while ([resultSet next]) {
        KLCurrencyEntity *entity = [[KLCurrencyEntity alloc] initWithFMResultSet:resultSet];
        [results addObject:entity];
    }
    [resultSet close];
    return results;
}

+ (NSArray<KLCurrencyEntity *> *)querySearchModelsWithSearchText:(NSString *)text withDataBase:(FMDatabase *)db {
    NSMutableArray *results = [@[] mutableCopy];
    if ([text length]){
        NSString *searchText = [NSString stringWithFormat:@"%%%@%%",text];
        FMResultSet *resultSet = [db executeQuery:@"select * from 'TB_CURRENCY_ENTITY' where name like ? or abbreviation like ?", searchText, searchText];
        while ([resultSet next]) {
            KLCurrencyEntity *entity = [[KLCurrencyEntity alloc] initWithFMResultSet:resultSet];
            [results addObject:entity];
        }
        [resultSet close];
    }
    return results;
}

@end
